package it.aekidna.onair.workflow
{
	import com.astoolkit.workflow.BasePipelineTask;
	
	import it.aekidna.onair.RunnableModuleEvent;
	import it.aekidna.onair.api.IRunnable;
	
	import mx.core.FlexGlobals;
	
	public class RunModuleTask extends BasePipelineTask
	{
		
		private var _module : IRunnable;
		
		override public function begin(inRetain:Boolean=false):void
		{
			super.begin( inRetain );
			_module = _pipelineData as IRunnable;
			if( !_module ) 
			{
				fail( "No module in the pipeline" );
				return;
			}
			if( _module.config.runOnStartup )
			{
				if( _module.run() )
					complete();
				else
				{
					_module.addEventListener( RunnableModuleEvent.INITIALIZED, onModuleInitialized );
				}
			}
			else
				complete();
			
		}
		
		private function onModuleInitialized( inEvent : RunnableModuleEvent ) : void
		{
			//FlexGlobals.topLevelApplication.exit();
			complete();
		}

	}
}